package com.wq.code;

/**
 * @Author: wangqiang20995
 * @Date:2019/5/21
 * @Description:
 * @Resource:
 */
public class ByteMove {

    private static final int MAXIMUM_CAPACITY = 1 << 30;

    public static void main(String[] args){
        System.out.println(judgeNumber(1323190422));
        System.out.println(powerOfTwo(65536));
        System.out.println(getSize(-2));
        System.out.println(7 ^ 9);

        System.out.println(tableSize(16));
        int size = 10;
        System.out.println( size++ > 10);//先取值比较，然后自增
        System.out.println(size);
        System.out.println(++size > 11);//先自增，然后取值比较

        System.out.println(isPowerOfTwo(8));

    }

    /**
     * 判断当前这个数的奇偶性
     * @param number
     * @return
     */
    private static boolean judgeNumber(int number){
        if(number == 0){
            throw new IllegalArgumentException("0既不是奇数也不是偶数");
        }

        return (number & 1) == 1;
    }

    private static boolean isPowerOfTwo(int number){
        return (number & -number) == number;
    }

    /**
     * 检查当前这个数是否是2的幂次
     * @param number
     * @return
     */
    private static boolean powerOfTwo(int number){
        if(number <= 0){
            throw new IllegalArgumentException("请输入一个正数");
        }
        return (number & (number - 1)) == 0;
    }

    /**
     * 统计一个数中的1的位数
     * @param num
     * @return
     */
    private static int getSize(int num){
        int i = 0;
        while (num != 0){
            i += num & 1;
            num = num >>> 1;
        }
        return i;
    }

    private static int tableSize(int cap){
        int n = cap - 1;
        n |= n >>> 1;
        n |= n >>> 2;
        n |= n >>> 4;
        n |= n >>> 8;
        n |= n >>> 16;
        return (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1;
    }
}
